package com.hzsk.lisence.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzsk.lisence.config.myexception.BaseException;
import com.hzsk.lisence.entity.EmProducePlanEntity;
import com.hzsk.lisence.entity.EmSoftwareRelationEntity;
import com.hzsk.lisence.entity.request.ProducePlanRequest;
import com.hzsk.lisence.mapper.EmProducePlanMapper;
import com.hzsk.lisence.mapper.EmSoftwareRelationMapper;
import com.hzsk.lisence.service.EmProducePlanService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description :
 * @Date : 2024/8/20
 * @Author : ChenJin
 * @Version : v1.0.0
 **/
@Service
public class EmProducePlanServiceImpl implements EmProducePlanService {
    
    @Resource
    private EmProducePlanMapper emProducePlanMapper;
    
    @Resource
    private EmSoftwareRelationMapper emSoftwareRelationMapper;

    @Override
    @Transactional
    public Boolean syncProducePlan(EmProducePlanEntity emProducePlanEntity) throws BaseException {
        if (ObjectUtils.isEmpty(emProducePlanEntity)){
            throw new BaseException("请求参数有误");
        }
        if (StringUtils.isNotEmpty(emProducePlanEntity.getOrderCode())){
            EmProducePlanEntity emProducePlan = emProducePlanMapper.selectProducePlanByOrderCode(emProducePlanEntity.getOrderCode());
            if (!ObjectUtils.isEmpty(emProducePlan)){
                emProducePlanMapper.updateProducePlanEntity(emProducePlanEntity);
                return true;
            }
        }
        emProducePlanMapper.insertProducePlanEntity(emProducePlanEntity);
        return true;
    }

    @Override
    public PageInfo<EmProducePlanEntity> getIdcPlanPage(ProducePlanRequest producePlanRequest) {
        PageHelper.startPage(producePlanRequest.getPageNum(), producePlanRequest.getPageSize());
        List<EmProducePlanEntity> emProducePlanEntityList = emProducePlanMapper.selectIdcPlanPage(producePlanRequest);
        return new PageInfo<>(emProducePlanEntityList);
    }

    @Override
    public PageInfo<EmProducePlanEntity> getNcPlanPage(ProducePlanRequest producePlanRequest) {
        PageHelper.startPage(producePlanRequest.getPageNum(), producePlanRequest.getPageSize());
        List<EmProducePlanEntity> emProducePlanEntityList = emProducePlanMapper.selectNcPlanPage(producePlanRequest);
        return new PageInfo<>(emProducePlanEntityList);
    }

    @Override
    public EmProducePlanEntity getIdcPlanDetail(Integer id) {
        return emProducePlanMapper.selectProducePlanById(id);
    }

    @Override
    public EmProducePlanEntity getNcPlanDetail(Integer id) {
        EmProducePlanEntity emProducePlan = emProducePlanMapper.selectProducePlanById(id);
        EmSoftwareRelationEntity emSoftwareRelationEntity = new EmSoftwareRelationEntity();
        emSoftwareRelationEntity.setNcMaterialCode(emProducePlan.getNcMaterialCode());
        emSoftwareRelationEntity.setMcpMaterialCode(emProducePlan.getMcpMaterialCode());
        EmSoftwareRelationEntity emSoftwareRelation = emSoftwareRelationMapper.selectSoftwareRelationByNcAndMcp(emSoftwareRelationEntity);
        if (ObjectUtils.isEmpty(emSoftwareRelation)){
            emSoftwareRelation = emSoftwareRelationMapper.selectSoftwareRelationByNc(emSoftwareRelationEntity);
        }
        if (!ObjectUtils.isEmpty(emSoftwareRelation)){
            emProducePlan.setOsVersion(emSoftwareRelation.getOsVersion());
            emProducePlan.setSoftwareVersion(emSoftwareRelation.getSoftwareVersion());
            emProducePlan.setResolution(emSoftwareRelation.getResolution());
        }
        return emProducePlan;
    }

    @Override
    public EmProducePlanEntity getProducePlan(String orderCOde) throws BaseException {
        EmProducePlanEntity emProducePlan = emProducePlanMapper.selectProducePlanByOrderCode(orderCOde);
        if (ObjectUtils.isEmpty(emProducePlan)){
            throw new BaseException("生产计划不存在");
        }
        EmSoftwareRelationEntity emSoftwareRelationEntity = new EmSoftwareRelationEntity();
        emSoftwareRelationEntity.setNcMaterialCode(emProducePlan.getNcMaterialCode());
        emSoftwareRelationEntity.setMcpMaterialCode(emProducePlan.getMcpMaterialCode());
        EmSoftwareRelationEntity emSoftwareRelation = emSoftwareRelationMapper.selectSoftwareRelationByNcAndMcp(emSoftwareRelationEntity);
        if (ObjectUtils.isEmpty(emSoftwareRelation)){
            emSoftwareRelation = emSoftwareRelationMapper.selectSoftwareRelationByNc(emSoftwareRelationEntity);
        }
        if (ObjectUtils.isEmpty(emSoftwareRelation)){
            throw new BaseException("软件绑定关系不存在");
        }
        emProducePlan.setOsVersion(emSoftwareRelation.getOsVersion());
        emProducePlan.setSoftwareVersion(emSoftwareRelation.getSoftwareVersion());
        emProducePlan.setOsId(emSoftwareRelation.getOsId());
        emProducePlan.setSoftwareId(emSoftwareRelation.getSoftwareId());
        emProducePlan.setResolution(emSoftwareRelation.getResolution());
        emProducePlan.setProdType(emSoftwareRelation.getProdType());
        emProducePlan.setChannelNum(emSoftwareRelation.getChannelNum());
        emProducePlan.setFunctions(emSoftwareRelation.getFunctions());
        return emProducePlan;
    }

    @Override
    public Boolean updatePlanStatus(EmProducePlanEntity emProducePlanEntity) {
        emProducePlanMapper.updateProducePlanStatus(emProducePlanEntity);
        return true;
    }

}
